//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Controls;
using CommunityServer.Components;


namespace CommunityServer.Controls
{
	/// <summary>
	/// Base CS control for managing Groups
	/// </summary>
	public abstract class GroupAdmin : TemplatedWebControl
	{

		#region ChildControls
		Label GroupID;
		TextBox GroupName;
		TextBox GroupDescription;
		Button Save;
		Button Delete;
		#endregion

		#region Core
		private bool _isEdit = false;
		protected virtual Boolean IsEdit
		{
			get { return _isEdit; }
			set { _isEdit = value; }
		}
		#endregion

		/// <summary>
		/// If not a page back, calls DataBind()
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				this.DataBind();
			}
			base.OnLoad(e);
		}

		/// <summary>
		/// Sets a default Skin name. All Applications should reuse this control and
		/// hopefully this skin as well.
		/// </summary>
		protected override String ExternalSkinFileName
		{
			get { return "Skin-GroupAdmin.ascx"; }
		}

		/// <summary>
		/// Returns a list of Groups
		/// </summary>
		/// <returns></returns>
		protected abstract ArrayList GetGroups();

		/// <summary>
		/// Property ResourceName (string)
		/// </summary>
		protected abstract string ResourceName
		{
			get;
		}

		protected override void AttachChildControls()
		{

			GroupID = (Label)FindControl("GroupID");
			GroupName = (TextBox)FindControl("GroupName");
			GroupDescription = (TextBox)FindControl("GroupDescription");

			ResourceLabel GroupAdminResourceLabel = FindControl("GroupAdminResourceLabel") as ResourceLabel;
			if (GroupAdminResourceLabel != null && ResourceName != null)
				GroupAdminResourceLabel.ResourceName = this.ResourceName;

			Save = (Button)FindControl("Save");
			Delete = (Button)FindControl("Delete");

			Save.Click +=new EventHandler(Save_Click);
			Delete.Click +=new EventHandler(Delete_Click);
		}

		protected virtual string DeleteResourceName
		{
			get{ return "CreateEditForums_DeleteForumVerify";}
		}

		/// <summary>
		/// Bind the current group (if available)
		/// </summary>
		public override void DataBind()
		{
			base.DataBind();

			Save.Text = CommunityServer.Components.ResourceManager.GetString("Save");
			Delete.Text = CommunityServer.Components.ResourceManager.GetString("Delete");
			Delete.Attributes["onclick"] = "if ( !confirm('" + ResourceManager.GetString(DeleteResourceName).Replace("'", @"\'") + "') ) {return false; } ";

			Group group = CurrentSectionGroup();
			if(group != null)
			{
				LoadGroup(group);
			}
		}

		protected abstract Group CurrentSectionGroup();

		protected abstract string RedirectUrl();

		protected virtual void LoadGroup(Group group)
		{
			Delete.Visible = !HasChildSections;
			GroupID.Text = group.GroupID.ToString();
			GroupName.Text = group.Name;
			GroupDescription.Text = group.Description;
		}

		protected abstract bool HasChildSections
		{
			get;
		}

		protected virtual bool UpdateGroup(Group group)
		{
			group.Name = GroupName.Text.Trim();
			group.Description = GroupDescription.Text.Trim();

			foreach(Group g in GetGroups())
			{
				if((g.Name == group.Name) && (g.GroupID != group.GroupID))
				{
					((RequiredFieldValidator)FindControl("GroupNameValidator")).IsValid = false;
					return false;
				}
			}

			if(Page.IsValid)
			{
				Groups.UpdateGroup(group);
				IsEdit = true;
				GetGroups();
				return true;
			}
			else
				return false;
		}

		protected virtual void DeleteGroup(Group group)
		{
			Groups.DeleteGroup(group);
		}

		private void Delete_Click(Object sender, EventArgs e)
		{
			Group group = CurrentSectionGroup();
			DeleteGroup(group);
			CSContext.Current.Context.Response.Redirect(RedirectUrl(), true);
		}

		private void Save_Click(Object sender, EventArgs e)
		{
			Group group = CurrentSectionGroup();
			if( UpdateGroup( group ) )
				CSContext.Current.Context.Response.Redirect(RedirectUrl(), true);
		}
	}
}
